﻿var XWFInteractiveState = {
    // 没有交互状态
    None: 0,
    // 绘制活动
    DrawActivity: 1,
    // 绘制动作
    DrawAction: 2,
    // 拖动活动
    DragActivity: 3,
};

// 渲染图形的视图
function XWFRenderView() {
    XWFView.apply(this, []);

    // 图形引擎
    this._engine = null;

    // 当前选中的元素
    this._selectedElement = null;

    // 将要绘制的元素
    this._drawElement = null;

    // 移动时最小的移动步长
    // 鼠标移动的值小于这个值都不会移动对象
    this._minMoveStep = 5;

    // 视图列表
    this._views = new Array();

    // 图表的种子号
    this._diagramSeed = 1;

    // 活动的种子号
    this._activitySeed = 1;

    // 元素蒙版
    // 拖动活动时提示用户新的位置
    // 绘制Activity时用于提示位置
    // 绘制Action时用于提示位置
    this._elementMask = null;

    // 鼠标按下时的坐标点，在拖动元素时用于记录第一次点下鼠标的位置。
    this._mouseDownPoint = null;

    // 交互状态
    this._interactiveState = XWFInteractiveState.None;

    //#override
    //安装渲染视图相关DOM元素
    this.onInstall = function (designer, callback) {
        this._domEl.style.overflow = 'scroll';
        XEngine.init(this._domEl);
        callback();
    }

    // 新建一个流程图
    this.new = function () {
        var name = 'workflow' + this._diagramSeed++;
        var diagram = XEngine.createDiagram(name);
        XEngine.showDiagram(diagram);
    };

    // 添加活动到流程图中
    this.addActivity = function (x, y, w, h, activity) {
        XEngine.getCurrentDiagram().add(activity);
        activity.setName('act' + this._activitySeed++);
        activity.setPos(x, y);
        var size = activity.getSize();
        if (w != null) {
            size.w = w;
        }
        if (h != null) {
            size.h = h;
        }
        activity.setSize(size.w, size.h);
    }

    this.getSelectedElement = function () {
        return this._selectedElement;
    };

    // 开始放置对象
    this.drawActivity = function (activityClass) {
        var activity = XWFActivityFactory.create(activityClass);
        if (activity != null) {
            this._interactiveState = XWFInteractiveState.DrawActivity;
            // 显示蒙版效果
            if (this._elementMask != null) {
                XEngine.getCurrentDiagram().remove(this._elementMask);
                XEngine.getCurrentDiagram().add(this._elementMask);
            } else {
                this._elementMask = new XWFActivityMask();
                XEngine.getCurrentDiagram().add(this._elementMask);
            }
            this._elementMask.setPos(0, 0);
            this._elementMask.setSize(activity.getSize().w, activity.getSize().h);
            this._drawElement = activity;
        }
    };

    // 开始绘制动作
    this.drawAction = function (actionClass) {
        if (this._interactiveState == XWFInteractiveState.DrawActivity) {
            this._drawElement = null;
            XEngine.getCurrentDiagram().remove(this._elementMask);
            this._elementMask = null;
        }

        // 设置为绘制Action的状态
        this._interactiveState = XWFInteractiveState.DrawAction;
        // 设置为Action的蒙版
        this._elementMask = new XWFActionMask();
        XEngine.getCurrentDiagram().add(this._elementMask);
    };

    // 鼠标点击事件
    this.onMouseDown = function (point) {
        console.log(this._interactiveState);
        if (this._interactiveState == XWFInteractiveState.DrawActivity) {
            // 验证坐标是否在图表范围内
            if (XUtil.pointInElement(point, XEngine.getDomEl())) {
                var pos = this._elementMask.getPos();

                // 放置元素
                this.addActivity(pos.x, pos.y, null, null, this._drawElement);
            }
            this._drawElement = null;

            // 取消蒙版
            XEngine.getCurrentDiagram().remove(this._elementMask);
            this._elementMask = null;
            // 重置交互模式
            this._interactiveState = XWFInteractiveState.None;
        } else if (this._interactiveState == XWFInteractiveState.DrawAction) {
            // 点击测试，获取鼠标坐标所在的活动
            var ptInDiagram = XEngine.pointToDiagram(point);
            var hitResult = XEngine.getCurrentDiagram().hitTest(ptInDiagram);
            if (hitResult.isHit) {
                // 设置点击到的元素为action蒙版的起始活动。
                this._elementMask.setFromActivity(hitResult.obj);
            } else {
                // 没有点击到activity上，视为无效。取消绘制Action的状态，设置为普通状态。
                this._elementMask = null;
                this._interactiveState = XWFInteractiveState.None;
            }
        } else {
            this._mouseDownPoint = point;

            var ptInDiagram = XEngine.pointToDiagram(point);
            var hitResult = XEngine.getCurrentDiagram().hitTest(ptInDiagram);
            if (hitResult.isHit) {
                if (this._selectedElement != hitResult.obj) {
                    this._selectedElement = hitResult.obj;

                    if (this._eventOnSelectedElementChanged != null) {
                        this._eventOnSelectedElementChanged(this._selectedElement);
                    }
                }

                if (this._selectedElement != null) {
                    // 显示蒙版效果
                    if (this._elementMask != null) {
                        XEngine.getCurrentDiagram().remove(this._elementMask);
                        XEngine.getCurrentDiagram().add(this._elementMask);
                    } else {
                        this._elementMask = new XWFActivityMask();
                        XEngine.getCurrentDiagram().add(this._elementMask);
                    }
                    this._elementMask.setPos(this._selectedElement.getPos().x,
                        this._selectedElement.getPos().y);
                    this._elementMask.setSize(this._selectedElement.getSize().w,
                        this._selectedElement.getSize().h);

                    // 设置为拖动模式
                    this._interactiveState = XWFInteractiveState.DragActivity;
                }
            }
        }
    };

    this.onMouseMove = function (point) {
        if (this._interactiveState == XWFInteractiveState.DrawActivity) {
            var ptInDiagram = XEngine.pointToDiagram(point);
            ptInDiagram.x = parseInt(ptInDiagram.x / this._minMoveStep) * this._minMoveStep;
            ptInDiagram.y = parseInt(ptInDiagram.y / this._minMoveStep) * this._minMoveStep;
            var size = this._elementMask.getSize();
            this._elementMask.setPos(ptInDiagram.x - parseInt(size.w / 2 / this._minMoveStep) * this._minMoveStep,
                ptInDiagram.y - parseInt(size.h / 2 / this._minMoveStep) * this._minMoveStep);
        } else if (this._interactiveState == XWFInteractiveState.DrawAction) {
            //设置action蒙版目标点信息，如果当前位置有活动这设置为Activity
            //如果当前鼠标位置没有Activity，则设置为当前的坐标点。
            //点击测试，获取鼠标坐标所在的活动
            var ptInDiagram = XEngine.pointToDiagram(point);
            var hitResult = XEngine.getCurrentDiagram().hitTest(ptInDiagram);
            if (hitResult.isHit) {
                // 设置activity为action蒙版的目标活动。
                this._elementMask.setToActivity(hitResult.obj);
            } else {
                this._elementMask.setToPos(ptInDiagram.x, ptInDiagram.y);
            }
        } else if (this._interactiveState == XWFInteractiveState.DragActivity) {
            var deltaX = point.x - this._mouseDownPoint.x;
            var deltaY = point.y - this._mouseDownPoint.y;
            deltaX = parseInt(deltaX / this._minMoveStep) * this._minMoveStep;
            deltaY = parseInt(deltaY / this._minMoveStep) * this._minMoveStep;
            var pos = this._selectedElement.getPos();
            this._elementMask.setPos(pos.x + deltaX, pos.y + deltaY);
        }
    }

    this.onMouseUp = function (point) {
        if (this._interactiveState == XWFInteractiveState.DragActivity) {
            var pos = this._elementMask.getPos();
            XEngine.getCurrentDiagram().remove(this._elementMask);
            this._elementMask = null;
            this._selectedElement.setPos(pos.x, pos.y);

            this._interactiveState = XWFInteractiveState.None;
        } else if (this._interactiveState == XWFInteractiveState.DrawAction) {
            var fromActivity = this._elementMask.getFromActivity();
            var toActivity = this._elementMask.getToActivity();
            if (fromActivity != null && toActivity != null) {
                var oAction = new XWFAction(fromActivity, toActivity);
                XEngine.getCurrentDiagram().add(oAction);
            }
            XEngine.getCurrentDiagram().remove(this._elementMask);
            this._elementMask = null;
            this._interactiveState = XWFInteractiveState.None;
        }
    }

    this._eventOnSelectedElementChanged = null;
    // 注册事件
    this.event = function (event, handler) {
        if (event == 'SelectedElementChanged') {
            this._eventOnSelectedElementChanged = handler;
        }
    }
}